json-rust
Parse and serialize JSON with ease.
Changelog - Complete Documentation - Cargo - Repository
Why?
JSON is a very loose format where anything goes - arrays can hold mixed types, object keys can change types between API calls or not include some keys under some conditions. Mapping that to idiomatic Rust structs introduces friction.
This crate intends to avoid that friction.
let parsed = parse.unwrap;
let instantiated = object!;
assert_eq!;
First class citizen
Using macros and indexing, it's easy to work with the data.
let mut data = object!;
// Partial equality is implemented for most raw types:
assert!;
// And it's type aware, `null` and `false` are different values:
assert!;
// But you can use any Rust number types:
assert!;
assert!;
assert!;
// Access nested structures, arrays and objects:
assert!;
assert!;
assert!;
// Error resilient - accessing properties that don't exist yield null:
assert!;
// Mutate by assigning:
data = "Hello".into;
// Use the `dump` method to serialize the data:
assert_eq!;
// Or pretty print it out:
println!;
Installation
Just add it to your Cargo.toml
file:
[]
= "*"
Then import it in your main.rs
/ lib.rs
file:
extern crate json;
Performance and Conformance
There used to be a statement here saying that performance is not the main goal of this crate. It is definitely one of them now.
While this crate doesn't provide a way to parse JSON to native Rust structs, it does a lot to optimize its performance for DOM parsing, stringifying and manipulation. It does very well in benchmarks, in some cases it can even outperform parsing to structs.
This crate implements the standard according to the RFC 7159 and ECMA-404 documents. For the best interoperability numbers are treated stored as 64bit precision mantissa with 16 bit decimal exponent for floating point representation.
License
This crate is distributed under the terms of both the MIT license and the Apache License (Version 2.0). Choose whichever one works best for you.
See LICENSE-APACHE and LICENSE-MIT for details.